﻿using System;
using System.Security.Principal;
using System.Web.Mvc;
using GiveAQuiz.Data;
using GiveAQuiz.Web.Authentication;
using GiveAQuiz.Web.Membership;
using GiveAQuiz.Web.Messenger;

namespace GiveAQuizSite.Controllers
{
  /// <summary>
  /// This abstract class provides property and methods for
  /// any controller needing to perform authentication 
  /// functionality.
  /// </summary>
  public abstract class AuthenticationController : BaseController
  {
    /// <summary>
    /// Base contructor to be called by concrete sub-classes
    /// of this class.
    /// </summary>
    /// <param name="repository">The QuizRepository.</param>
    /// <param name="formsAuth">The Forms Authentication module.</param>
    /// <param name="membershipService">The Membership Service.</param>
    /// <param name="acctMessenger">The Account Messenger service.</param>
    protected AuthenticationController(IQuizRepository repository, 
      IFormsAuthentication formsAuth,
      IMembershipService membershipService,
      IAccountMessenger acctMessenger) : base(repository)
    {
      FormsAuth = formsAuth;
      MembershipService = membershipService;
      AccountMessenger = acctMessenger;
    }

    public IFormsAuthentication FormsAuth
    {
      get;
      protected set;
    }

    public IMembershipService MembershipService
    {
      get;
      protected set;
    }

    public IAccountMessenger AccountMessenger
    {
      get;
      protected set;
    }

    /// <summary>
    /// Event handler for attaching to the ActionExecuting event.
    /// </summary>
    /// <param name="filterContext">The ActionExecutingContext.</param>
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      if (filterContext.HttpContext.User.Identity is WindowsIdentity)
      {
        throw new InvalidOperationException("Windows authentication is not supported.");
      }
    }

  }
}
